﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test_API
{
    public static class API_VERDICT
    {
        public const string PARSING_FAILURE = "/* JSON PARSING ERROR */";

        public static bool checkStatusFieldOnly(string json, ref LogEntry entry)
        {
            try
            {
                Dictionary<string, object> htmlAttributes = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                if (htmlAttributes["Status"] == null)
                {
                    throw new Exception("Status field was not found in the returned JSON");
                }
                entry.verdict = Boolean.Parse(htmlAttributes["Status"].ToString()) == entry.expectedResult;

                if (entry.verdict == false)
                {
                    bool tokenExpiredId = htmlAttributes.ContainsKey("errorId") && htmlAttributes.ContainsKey("errorDescription");
                    string errorId = tokenExpiredId ? (string)htmlAttributes["errorId"] : String.Empty;

                    if (tokenExpiredId && errorId.ToUpper() == "A27A9FDE-8E94-4F58-9EFB-8C4B886B1A27")
                    {
                        Console.WriteLine("Error: {0}.\nPlease collect a new token and start again. Program will exit now...\n", htmlAttributes["errorDescription"]);
                        Environment.Exit(0);
                    }
                }
            }
            catch (Exception e)
            {
                entry.verdict = false;
                entry.receivedData = API_VERDICT.PARSING_FAILURE + " && " + e.Message + entry.receivedData;
            }
            return entry.verdict;
        }

        public static bool checkAddOrUpdateOperation(string json, bool doAddOperation)
        {
            bool verdict = false;
            try
            {
                JObject receivedJson = JObject.Parse(json);
                long creationTime = (long)receivedJson.SelectToken("creationTimestamp");
                long updateTime = (long)receivedJson.SelectToken("modifiedTimestamp");
                if (doAddOperation)
                {
                    verdict = creationTime == updateTime || updateTime == creationTime + 1;
                }
                else
                    verdict = updateTime > creationTime;
            }
            catch (Exception)
            {
                verdict = false;
            }
            return verdict;
        }

        /// <summary>
        /// Throws an exception if folder is not created
        /// </summary>
        public static bool checkFolderIsCreated(ref LogEntry tViewAll, string errorExplanation)
        {
            try
            {
                if (tViewAll.verdict==false)
                {
                    tViewAll.receivedData = "/* " + errorExplanation + " */" + tViewAll.receivedData;
                    return false;
                }
                Dictionary<string, object> htmlAttributes = JsonConvert.DeserializeObject<Dictionary<string, object>>(tViewAll.receivedData);
                if (htmlAttributes.ContainsKey("selfUrl") == false)
                {
                    tViewAll.verdict = false;
                    tViewAll.receivedData = "/* " + errorExplanation + " */" + tViewAll.receivedData;
                }
                return true;
            }
            catch (Exception e)
            {
                tViewAll.verdict = false;
                if(e is JsonException)  
                    tViewAll.receivedData = API_VERDICT.PARSING_FAILURE + tViewAll.receivedData;
                return false;
            }
        }

        public static bool checkAtleasetOneElementIsPresent(LogEntry entry)
        {
            JObject responseEntries = JObject.Parse(entry.receivedData);
            if (responseEntries["dataSet"] == null)
            {
                return false;
            }
            return ((JArray)responseEntries["dataSet"]).Count > 0;
        }
    }
}
